package com.ai.smlcore.util;

import java.io.InputStream;
import java.util.Properties;
import java.util.Vector;

import org.apache.commons.io.IOUtils;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

/**
 */
public class MySFtp {

	/**
	 * 连接sftp服务器
	 * 
	 * @param host
	 *            主机
	 * @param port
	 *            端口
	 * @param username
	 *            用户名
	 * @param password
	 *            密码
	 * @return
	 */
	public ChannelSftp connect(String host, int port, String username,
			String password) {
		ChannelSftp sftp = null;
		try {
			JSch jsch = new JSch();
			jsch.getSession(username, host, port);
			Session sshSession = jsch.getSession(username, host, port);
			System.out.println("Session created.");
			sshSession.setPassword(password);
			Properties sshConfig = new Properties();
			sshConfig.put("StrictHostKeyChecking", "no");
			sshSession.setConfig(sshConfig);
			sshSession.connect();
			System.out.println("Session connected.");
			System.out.println("Opening Channel.");
			Channel channel = sshSession.openChannel("sftp");
			channel.connect();
			sftp = (ChannelSftp) channel;
			System.out.println("Connected to " + host + ".");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sftp;
	}

	/**
	 * 上传文件
	 * 
	 * @param directory
	 *            上传的目录
	 * @param uploadFile
	 *            要上传的文件
	 * @param sftp
	 */
	public void upload(String fileName, InputStream inputStream, ChannelSftp sftp) {
		try {
			sftp.put(inputStream, fileName);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 下载文件
	 * 
	 * @param directory
	 *            下载目录
	 * @param downloadFile
	 *            下载的文件
	 * @param saveFile
	 *            存在本地的路径
	 * @param sftp
	 */
	public InputStream download(String downloadFile ,ChannelSftp sftp) throws Exception {
		return sftp.get(downloadFile);
	}

	/**
	 * 列出目录下的文件
	 * 
	 * @param directory
	 *            要列出的目录
	 * @param sftp
	 * @return
	 * @throws SftpException
	 */
	public Vector listFiles(String directory, ChannelSftp sftp)
			throws SftpException {
		return sftp.ls(directory);
	}

	public static void main(String[] args) throws Exception {
		
		MySFtp sf = new MySFtp();
		String host = "221.176.67.103";
		int port = 5000;
		String username = "work";
		String password = "W0rk@2015";
		String downloadFile = "a.txt";
		ChannelSftp sftp = sf.connect(host, port, username, password);
		System.out.println(sftp);
		System.out.println(sftp.getHome());
		sftp.cd("upload");
		
		InputStream inputStream = sf.download(downloadFile, sftp);
		String string = IOUtils.toString(inputStream);
		System.out.println(string);
		
		Vector list = sf.listFiles(sftp.getHome()+"/upload", sftp);
		for (Object object : list) {
			if (object instanceof LsEntry) {
				LsEntry entry = (LsEntry) object;
				System.out.println(entry.getFilename());
			}
		}
		sf.upload("nihao.txt", inputStream, sftp);
		
		
		
	}
}